package cn.shadow.module.chess.message;

import cn.shadow.bean.PlayerModule;
import cn.shadow.module.chess.message.chnChess.frame.ChnChessGame;
import cn.shadow.module.chess.message.chnChess.frame.ChnChessScreen;
import cn.shadow.moulde.AHandler;
import cn.shadow.moulde.AModule;
import cn.shadow.moulde.Module;
import cn.shadow.proto.client.CHnChessClientProtoContent.*;
import cn.shadow.proto.client.ChessClientProtoContent.*;
import cn.shadow.utils.BufferUtil;
import io.netty.buffer.ByteBuf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Scanner;

/**
 * Created time:2020-08-19 10:53:27
 */
@AModule(ChessModuleMessages.MODULE_ID)
@Component
public class ChessModule extends Module {

    @Autowired
    private Scanner scanner;

    @AHandler(ChessModuleMessages.S2C_CHESS_INVITE_PLAYER)
    public void invitePlayer(ByteBuf buffer, PlayerModule module) throws Exception {
        // 以下代码为自动生成，请不要随意修改
        final byte[] invitePlayer = BufferUtil.readBytes(buffer);  //被邀请信息
        // 请在下面添加你的代码
        InvitePlayer msg = InvitePlayer.parseFrom(invitePlayer);
        int gameType = msg.getGameType();
        String inviterName = msg.getInviterName();
        String gameId = msg.getGameId();
        long inviteeId = msg.getInviteeId();
        System.out.println(inviterName+"邀请您进行一场游戏，类型为："+gameType+"是否同意，请选择");
        String agree = scanner.nextLine();
        if (agree.equals("y")) {
            System.out.println("您接受了邀请，等待游戏开始");
            module.sendMessage(ChessModuleMessages.AcceptMsg(gameType, gameId, inviteeId));
        } else {
            System.out.println("您拒绝了邀请，等待游戏开始");
            module.sendMessage(ChessModuleMessages.AcceptMsg(gameType, "0", inviteeId));
        }
    }
    @AHandler(ChessModuleMessages.S2C_CHESS_ACCEPT)
    public void accept(ByteBuf buffer, PlayerModule module) throws Exception {
        // 以下代码为自动生成，请不要随意修改
        final int chessType  = BufferUtil.readVarInt32(buffer);  //游戏类型
        final String gameId  =  BufferUtil.readUTF(buffer);  //游戏id
        final String inviteeName  =  BufferUtil.readUTF(buffer);  //被邀请者
        final boolean accept  =  BufferUtil.readBoolean(buffer);  //接受或拒绝
        if (accept) {
            moduleLog.info(String.format( "%s同意了你的邀请,开始游戏吧",inviteeName));
            module.sendMessage(ChessModuleMessages.StartMsg(chessType, gameId));
        } else {
        moduleLog.info(String.format( "%s拒绝了你的邀请",inviteeName));
        }
        // 请在下面添加你的代码
    }

    @AHandler(ChessModuleMessages.S2C_CHESS_START)
    public void start(ByteBuf buffer, PlayerModule module) throws Exception {
        // 以下代码为自动生成，请不要随意修改
        final byte[] initBoardBytes = BufferUtil.readBytes(buffer);  //初始化棋盘和玩家消息
        // 请在下面添加你的代码
        InitBoard initBoard = InitBoard.parseFrom(initBoardBytes);
        int gameId = initBoard.getGameId();
        int gameType = initBoard.getGameType();
        moduleLog.info(String.format("创建游戏界面type=%s,id=%s", gameType, gameId));
//        ChnChessScreen chnChessScreen = new ChnChessScreen(chnChessGame);
//        chnChessGame.setScreen(chnChessScreen);
    }

    @Autowired
    ChnChessGame chnChessGame;

}
